#!/bin/sh

set -eu

INSTALL_DIR="$JIAPKG_INSTALL_DIR"
JOBS="$JIAPKG_JOBS"
JIAPKG="$JIAPKG_EXE"

pkg_config_path() {
	printf '%s/lib/pkgconfig' "$($JIAPKG info --install-dir zlib)"
	printf ':'
	printf '%s/lib/pkgconfig' "$($JIAPKG info --install-dir sqlite)"
}

jiapkg_build() {
	PKG_CONFIG_PATH="$(pkg_config_path)"
	PKG_CONFIG=$(which pkgconf)

	export PKG_CONFIG_PATH
	export PKG_CONFIG

	# Add RUNPATH to the module (shared library) linked with sqlite3.
	LIBSQLITE3_LIBS="$(pkgconf --libs sqlite3) -Wl,rpath=$(pkgconf --variable=libdir sqlite3)"
	export LIBSQLITE3_LIBS

	./configure --prefix="$INSTALL_DIR" --enable-optimizations
	make -j"$JOBS"
}

jiapkg_install() {
	make install
}

case "$1" in
build) jiapkg_build ;;
install) jiapkg_install ;;
esac
